#ifndef __TimerDef_H
#define __TimerDef_H

#ifdef APM32E10X_HD
	#include "apm32e10x_tmr.h"
	#include "apm32e10x_misc.h"
#elif defined AT32F403AVGT7
	#include "at32f403a_407_tmr.h"	
	#include "at32f403a_407_misc.h"  
#else
	#include "apm32f10x_tmr.h"
	#include "apm32f10x_misc.h"
#endif

#include "PWM_Control.h"
#include "stdio.h"

#include "SQDriConfig.h"


// 时钟计数器溢出中断标志;
#ifdef AT32F403AVGT7
#define  Tmr_Default_INT_FLG                   TMR_OVF_FLAG
#else
#define  Tmr_Default_INT_FLG                   TMR_INT_UPDATE
#endif //#ifdef AT32F403AVGT7

// RTC计时时钟;
#if RTC_ON == 0 || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
#define CLOCK_RCM_EnableAPBnPeriphClock        RCM_EnableAPB1PeriphClock
#define Tmr_CLOCK_RTC                          RCM_APB1_PERIPH_TMR2
#define Tmr_CLOCK_Id                           TMR2
#define Tmr_CLOCK_IRQn                         TMR2_IRQn
#define Tmr_CLOCK_IRQHandler                   TMR2_IRQHandler
	#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay && RTC_ON == 0
	  // 需要2个定时器; 一个时钟定时器，一个CP_CarD1_Trigger_TMRDelay的定时器;
		// 这里需要再安排个定时器，用作CP_CarD1_Trigger_TMRDelay延时。
		 
  
  
  
	#endif
#endif

#ifndef UsingFreeRTOS
	#if RlyPin_Ctrl_PWM_EN > 0 && (defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW)
	  // 执行任务时钟：
	  #define Task_RCM_EnableAPBnPeriphClock         RCM_EnableAPB1PeriphClock
		#define Tmr_Task_RTC                           RCM_APB1_PERIPH_TMR5
		#define Tmr_Task_Id                            TMR5
		#define Tmr_Task_IRQn                          TMR5_IRQn
		#define Tmr_Task_IRQHandler                    TMR5_IRQHandler
	#else
	  // 执行任务时钟：
	  #define Task_RCM_EnableAPBnPeriphClock         RCM_EnableAPB1PeriphClock
		#define Tmr_Task_RTC                           RCM_APB1_PERIPH_TMR4
		#define Tmr_Task_Id                            TMR4
		#define Tmr_Task_IRQn                          TMR4_IRQn
		#define Tmr_Task_IRQHandler                    TMR4_IRQHandler
	#endif
#endif //#ifndef UsingFreeRTOS

#if RlyPin_Ctrl_PWM_EN > 0 
  #if (defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW)
		// 执行PWM控制继电器时钟：
		#define Relay_RCM_EnableAPBnPeriphClock        RCM_EnableAPB1PeriphClock
		#define Tmr_Relay_RTC                          RCM_APB1_PERIPH_TMR4
		#define Tmr_Relay_Id                           TMR4
		#define Tmr_Relay_IRQn                         TMR4_IRQn
		#define Tmr_Relay_IRQHandler                   TMR4_IRQHandler
		
		// PWM的占空比控制：
		#define Relay_TMR_ConfigOCn                    TMR_ConfigOC2
		#define Relay_TMR_ConfigOCnPreload             TMR_ConfigOC2Preload
		#define Relay_ConfigCompareSetn                TMR_ConfigCompare2
		#ifdef AT32F403AVGT7
			#define Relay_INT_FLAG                       TMR_C2_FLAG
		#else
			#define Relay_INT_FLAG                       TMR_INT_CC2
		#endif
		
	#else	
		// 执行PWM控制继电器时钟：
		#define Relay_RCM_EnableAPBnPeriphClock        RCM_EnableAPB2PeriphClock
		#define Tmr_Relay_RTC                          RCM_APB2_PERIPH_TMR1
		#define Tmr_Relay_Id                           TMR1
		#define Tmr_Relay_IRQn                         TMR1_IRQn
		#define Tmr_Relay_IRQHandler                   TMR1_IRQHandler
		
		// PWM的占空比控制：
		#define Relay_TMR_ConfigOCn                    TMR_ConfigOC1
		#define Relay_TMR_ConfigOCnPreload             TMR_ConfigOC1Preload
		#define Relay_ConfigCompareSetn                TMR_ConfigCompare1
		#ifdef AT32F403AVGT7
			#define Relay_INT_FLAG                       TMR_C1_FLAG
		#else
			#define Relay_INT_FLAG                       TMR_INT_CC1
		#endif
			
  #endif
	
	

#endif

#if defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW
	// ADC数据提取时钟 = Tmr3
	#define ADC_RCM_EnableAPBnPeriphClock          RCM_EnableAPB1PeriphClock  
	#define Tmr_ADC_RTC                            RCM_APB1_PERIPH_TMR3  
	#define Tmr_ADC_Id                             TMR3 
	#define Tmr_ADC_IRQn                           TMR3_IRQn 
	#define TmrAdc_IRQHandler                      TMR3_IRQHandler  

#else
	// ADC数据提取时钟 = Tmr1
	#define ADC_RCM_EnableAPBnPeriphClock          RCM_EnableAPB2PeriphClock  
	#define Tmr_ADC_RTC                            RCM_APB2_PERIPH_TMR1 
	#define Tmr_ADC_Id                             TMR1 
	#define Tmr_ADC_IRQn                           TMR1_UP_IRQn 
	#define TmrAdc_IRQHandler                      TMR1_UP_IRQHandler 

#endif //#ifdef SQDri_EVSEB_7KW

#if defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW 
// PWM的时钟源:Timer1 CH1做时钟源: PA8
#define PWM_RCM_EnableAPBnPeriphClock          RCM_EnableAPB2PeriphClock
#define Tmr_PWM_RTC                            RCM_APB2_PERIPH_TMR1
#define Tmr_PWM_Id                             TMR1

#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CCPWMUPDT  || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
#define PWM_TMR_UP_IRQn                        TMR1_UP_IRQn
#define PWM_TMR_UP_IRQHandler                  TMR1_UP_IRQHandler
#define PWM_TMR_UP_FLAG                        TMR_INT_UPDATE
#endif

// PWM中断函数通道：
#define Tmr_PWM_IRQn                           TMR1_CC_IRQn
#define Tmr_PWM_IRQHandler                     TMR1_CC_IRQHandler

// PWM的时钟通道3 输出PWM波形
#define PWM_TMR_ConfigOCn                      TMR_ConfigOC1
#define PWM_TMR_ConfigOCnPreload               TMR_ConfigOC1Preload
#define TMR_ConfigCompareSetn                  TMR_ConfigCompare1
#ifdef AT32F403AVGT7
#define PWM_CC_INT_FLAG                        TMR_C1_FLAG
#else
#define PWM_CC_INT_FLAG                        TMR_INT_CC1
#endif

#if CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_CC50CC950
// PWM的时钟通道1 驱动 ADC采样
#define ADC_TMR_ConfigOCn                      TMR_ConfigOC3
#define ADC_TMR_ConfigOCnPreload               TMR_ConfigOC3Preload
#define ADC_ConfigCompareSetn                  TMR_ConfigCompare3
#ifdef AT32F403AVGT7
#define ADC_INT_FLAG                           TMR_C3_FLAG
#else
#define ADC_INT_FLAG                           TMR_INT_CC3
#endif


// PWM的时钟通道2 驱动 ADC采样
#define CarD1_TMR_ConfigOCn                    TMR_ConfigOC2
#define CarD1_TMR_ConfigOCnPreload             TMR_ConfigOC2Preload
#define CarD1_ConfigCompareSetn                TMR_ConfigCompare2
#ifdef AT32F403AVGT7
	#define CarD1_INT_FLAG                       TMR_C2_FLAG
#else
	#define CarD1_INT_FLAG                       TMR_INT_CC2
#endif
#endif

#else
// PWM的时钟源:Timer3 CH3做时钟源
#define PWM_RCM_EnableAPBnPeriphClock          RCM_EnableAPB1PeriphClock
#define Tmr_PWM_RTC                            RCM_APB1_PERIPH_TMR3
#define Tmr_PWM_Id                             TMR3

// PWM中断函数通道：
#define Tmr_PWM_IRQn                           TMR3_IRQn
#define Tmr_PWM_IRQHandler                     TMR3_IRQHandler

// PWM的时钟通道3 输出PWM波形
#define PWM_TMR_ConfigOCn                      TMR_ConfigOC3
#define PWM_TMR_ConfigOCnPreload               TMR_ConfigOC3Preload
#define TMR_ConfigCompareSetn                  TMR_ConfigCompare3

// PWM的时钟通道1 驱动 ADC采样
#define ADC_TMR_ConfigOCn                      TMR_ConfigOC1
#define ADC_TMR_ConfigOCnPreload               TMR_ConfigOC1Preload
#define ADC_ConfigCompareSetn                  TMR_ConfigCompare1
#ifdef AT32F403AVGT7
	#define ADC_INT_FLAG                         TMR_C1_FLAG
#else
	#define ADC_INT_FLAG                         TMR_INT_CC1
#endif

// PWM的时钟通道2 驱动 ADC采样
#define CarD1_TMR_ConfigOCn                    TMR_ConfigOC2
#define CarD1_TMR_ConfigOCnPreload             TMR_ConfigOC2Preload
#define CarD1_ConfigCompareSetn                TMR_ConfigCompare2
#ifdef AT32F403AVGT7
	#define CarD1_INT_FLAG                       TMR_C2_FLAG
#else
	#define CarD1_INT_FLAG                       TMR_INT_CC2
#endif

#endif


void Tmr_PWM_init(void);
void PWM_Duty_Set(uint16_t dutyValue);

#if RTC_ON == 0 || CP_CarD1_Trigger_Mode == CP_CarD1_Trigger_TMRDelay
void Tmr_CLOCK_init(void);
#endif

#if UI_Meter_Method == Meter_SelfADC || CP_Mode == CP_Mode_sta
void Tmr_ADC_init(void);
#endif

void Tmr_Task_init(void);

#if RlyPin_Ctrl_PWM_EN > 0
#if defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW 
void Tmr_Relay_init(void);
#endif
void Relay_Duty_Set(uint16_t dutyValue);
#endif

#endif
